var express = require('express');
var router = express.Router();
const { login, changeAdminService } = require("../service/adminService");
const { formatResponseBody } = require("../utils/tools");
const { validationCaptcha } = require("../service/captchaService");

// 登录
router.post('/login', async (req, res, next) => {
    // 1. 验证验证码
    validationCaptcha(req);

    // 2. 交由服务层处理
    res.send(await login(req.body, res));
});

// 恢复登录
router.get("/whoami", (req, res, next) => {
    // 如果能中间件能走到这里，证明验证成功
    res.send(formatResponseBody({ data: req.token }));
})

// 修改管理员信息
router.put("/", async (req, res, next) => {
    // 进行修改管理员信息业务处理
    await changeAdminService(req.body, req.token.id);

    res.send(formatResponseBody({
        data: {
            id: req.token.id,
            name: req.body.name,
            loginid: req.body.loginId
        }
    }))
})

module.exports = router;